<?php
/**
 * 装备
 * 
 * Created  : 2011-05-30
 * Modified : 2011-05-30
 * @author  : 陈立强
 */
class Equip
{
	public static $cfg = null;
	public static $cfgName = 'item/equip';
	
	/**
	 * 获取一件装备的信息
	 * @param int $uid
	 * @param int $equip_id
	 */
	public static function getInfo($uid, $equip_id, $additionShow=false)
	{
		global $DB, $L, $G;
		
		$sql = 'SELECT * FROM bag WHERE bag_id='.(int)$equip_id;
		$info = $DB->getValue($sql);
		if (!$info || $info['uid']!=$uid) {
			return array(
				'val' => false,
				'msg' => $L['invalid_request']
			);
		}
		
		$info['attr'] = json_decode($info['attr'], true) ;
		
		$info['inlay'] = json_decode($info['inlay'], true) ;
		
		return $info;
	}
	
	
	/**
	 * 获取装备员工身上的装备
	 * @param int $uid
	 * @param int $status 状态ID 1-背包中装备,3-背包和身上装备
	 * @param int $part 装备部位
	 */
	public static function getEquipList($uid, $status=1, $part=0)
	{
		global $DB, $L, $G;
		
		$staff_id = (int)$staff_id;
		if ($status == 1) {
			$status = 'status=1';
		} else if ($status == 2) {
			$status = 'status=2';
		} else {
			$status = 'status in (1,2)';
		}
		if ($part) {
			$part = 'AND part='.(int)$part;
		} else {
			$part = '';
		}
		$sql = 'SELECT * FROM bag WHERE uid='.$uid.' AND item_type=53 '.$part.' AND '.$status;
		$list = $DB->getList($sql);
		$cfgList = C('item/equip');
		foreach ($list as $k=>$item) {
			$cfg = $cfgList[$item['item_id']];
			$list[$k]['make_class'] = $cfg['class'];
			$list[$k]['need_staff_lvl'] = $cfg['need_staff_lvl'];
			$list[$k]['attr'] = json_decode($item['attr'], true);
			$list[$k]['inlay'] = json_decode($item['inlay'], true);
		}
		
		return $list;
	}
	
	/**
	 * 获取装备员工身上的装备
	 * @param int $uid
	 * @param int $staff_id 员工ID
	 */
	public static function getEquipOnBodyList($uid, $staff_id)
	{
		global $DB, $L, $G;
		
		$staff_id = (int)$staff_id;
		$sql = 'SELECT * FROM bag WHERE staff_id='.$staff_id.' AND status=2';
		$list = $DB->getList($sql);
		$cfgList = C('item/equip');
		$_list = array();
		foreach ($list as $item) {
			$cfg = $cfgList[$item['item_id']];
//			$list[$k]['make_class'] = $cfg['class'];
			$item['need_staff_lvl'] = $cfg['need_staff_lvl'];
			$item['attr'] = json_decode($item['attr'], true);
			$item['inlay'] = json_decode($item['inlay'], true);
			$_list[$item['part']] = $item;
		}
		
		return $_list;
	}
	
	/**
	 * 穿戴
	 * @param int $uid
	 * @param int $staff_id
	 * @param int $equip_id 装备在背包中的ID
	 */
	public static function putOn($uid, $equip_id)
	{
		global $DB, $L, $G;
		
		$equip_id = (int)$equip_id;
		
		$info = self::getInfo($uid, $equip_id);
		if (!$info || $info['uid']!=$uid) {
			return array(
				'val' => false,
				'msg' => $L['invalid_request']
			);
		}
		// 非装备不能穿戴
		if ($info['item_type']!=53) {
			return array(
				'val' => false,
				'msg' => $L['invalid_request']
			);
		}
		// 装备不在背包
		if ($info['status']!=1) {
			return array(
				'val' => false,
				'msg' => $L['invalid_request']
			);
		}
		// 装备配置
		$cfg = self::$cfg[$info['item_id']];
		$staffInfo = Staff::getInfo($uid, $cfg['class']);
		if (!$staffInfo) {
			return array(
				'val' => false,
				'msg' => $L['equip_class_staff_not_exist']
			);
		}
		$staff_id = $staffInfo['staff_id'];
		
		// 该员工不是当前玩家的
		if (!$staffInfo || $staffInfo['uid']!=$uid) {
			return array(
				'val' => false,
				'msg' => $L['invalid_request']
			);
		}
		// 穿戴装备的员工等级达不到要求
		if ($cfg['need_staff_lvl'] > $staffInfo['potential_lvl']) {
			return array(
				'val' => false,
				'msg' => $L['staff_potential_lvl_not_enough']
			);
		}
		// 当前员工身上同部位的装备
		$sql = 'SELECT * FROM bag WHERE staff_id='.$staff_id.' AND part='.$info['part']
				.' AND status=2';
		$onInfo = $DB->getValue($sql);
		
		$DB->beginTransaction();
		
		// 装备穿上
		$data = array(
			'status' => 2,
			'staff_id' => $staff_id
		);
		$where = 'bag_id='.$info['bag_id'];
		$ret = $DB->update('bag', $data, $where);
		
		// 脱下身上装备
		if ($ret && $onInfo) {
			// 原来装备脱下
			$data = array(
				'status' => 1
			);
			$where = 'bag_id='.$onInfo['bag_id'];
			$ret = $DB->update('bag', $data, $where);
			// 脱下装备影响的属性
			$onInfo['attr'] = json_decode($onInfo['attr'], true);
			$onInfo['inlay'] = json_decode($onInfo['inlay'], true) ;
			$inlay = array();
			if ($onInfo['inlay']) {
				foreach ($onInfo['inlay'] as $k=>$v) {
					if (count($v) == 0) {
						continue;
					}
					$abi_id = key($v);
					$val = $v[$abi_id];
					if (isset($inlay[$abi_id])) {
						$inlay[$abi_id] += $val;
					} else {
						$inlay[$abi_id] = $val;
					}
				}
			}
			$sql = '';
			foreach ($onInfo['attr']['result'] as $abi_id=>$abi_val) {
					$sql .= 'UPDATE staff_data SET abi_val=abi_val-'.$abi_val
						 .' WHERE uid='.$uid.' AND abi_id='.$abi_id.';';
			}
			foreach ($inlay as $abi_id => $val) {
				$sql .= 'UPDATE staff_data SET abi_val=abi_val-'.$val
					 .' WHERE uid='.$uid.' AND abi_id='.$abi_id.';';
			}
			$ret = $DB->exeSql($sql);
			// 同时更新宝石镶嵌对应的品质属性
		}
		// 更新员工属性
		if ($ret) {
			if (is_string($info['inlay'])) {
				$info['inlay'] = json_decode($info['inlay'], true) ;
			}
			$inlay = array();
			if ($info['inlay']) {
				foreach ($info['inlay'] as $k=>$v) {
					if (count($v) == 0) {
						continue;
					}
					$abi_id = key($v);
					$val = $v[$abi_id];
					if (isset($inlay[$abi_id])) {
						$inlay[$abi_id] += $val;
					} else {
						$inlay[$abi_id] = $val;
					}
				}
			}
			$sql = '';
			if ($info['attr']) {
				foreach ($info['attr']['result'] as $abi_id=>$abi_val) {
					$sql .= 'UPDATE staff_data SET abi_val=abi_val+'.$abi_val
						 .' WHERE uid='.$uid.' AND abi_id='.$abi_id.';';
				}
			}
			foreach ($inlay as $abi_id => $val) {
				$sql .= 'UPDATE staff_data SET abi_val=abi_val+'.$val
					 .' WHERE uid='.$uid.' AND abi_id='.$abi_id.';';
			}
			$ret = $DB->exeSql($sql);
		}
		
		$result['val'] = (boolean)$ret;
		if ($ret) {
			$DB->commit();
		} else {
			$DB->rollBack();
			$result['msg'] = isset($msg) ? $msg : $L['Failure'];
			$str = var_export($DB->errors, true);
			// 记录错误日志
			Log::save('equip_put_on', $str);
		}
		return $result;
	}
	
	/**
	 * 脱下装备
	 * @param int $uid
	 * @param int $staff_id 员工ID
	 * @param int $equip_id 装备在背包中的ID
	 */
	public static function takeOff($uid, $equip_id)
	{
		global $DB, $L, $G;
		
		$equip_id = (int)$equip_id;
		
		$sql = 'SELECT * FROM bag WHERE bag_id='.$equip_id;
		$onInfo = $DB->getValue($sql);
		if (!$onInfo || $onInfo['uid']!=$uid) {
			return array(
				'val' => false,
				'msg' => $L['invalid_request']
			);
		}
		// 非装备不能穿戴
		if ($onInfo['item_type']!=53) {
			return array(
				'val' => false,
				'msg' => $L['invalid_request']
			);
		}
		// 装备不在身上
		if ($onInfo['status']!=2) {
			return array(
				'val' => false,
				'msg' => $L['invalid_request']
			);
		}
		
		// 判断背包空间是否足够
		$freeGrid = Bag::getFreeGrid($uid);
		if (!$freeGrid) {
			return array(
				'val' => false,
				'msg' => $L['not_enough_grid_in_bag']
			);
		}
		
		$cfg = self::$cfg[$onInfo['item_id']];
		$staffInfo = Staff::getInfo($uid, $cfg['class']);
		if (!$staffInfo) {
			return array(
				'val' => false,
				'msg' => $L['equip_class_staff_not_exist']
			);
		}
		$staff_id = $staffInfo['staff_id'];
		
		$DB->beginTransaction();
		
		$data = array(
			'status' => 1
		);
		$where = 'bag_id='.$onInfo['bag_id'];
		$ret = $DB->update('bag', $data, $where);
		
		// 更新员工属性
		if ($ret) {
			$onInfo['inlay'] = json_decode($onInfo['inlay'], true) ;
			$inlay = array();
			if ($onInfo['inlay']){
				foreach ($onInfo['inlay'] as $k=>$v) {
					if (count($v) == 0) {
						continue;
					}
					$abi_id = key($v);
					$val = $v[$abi_id];
					if (isset($inlay[$abi_id])) {
						$inlay[$abi_id] += $val;
					} else {
						$inlay[$abi_id] = $val;
					}
				}
			}
			$sql = '';
			$onInfo['attr'] = json_decode($onInfo['attr'], true);
			if ($onInfo['attr']) {
				foreach ($onInfo['attr']['result'] as $abi_id=>$abi_val) {
					$sql .= 'UPDATE staff_data SET abi_val=abi_val-'.$abi_val
						 .' WHERE uid='.$uid.' AND abi_id='.$abi_id.';';
				}
			}
			foreach ($inlay as $abi_id => $val) {
				$sql .= 'UPDATE staff_data SET abi_val=abi_val-'.$val
					 .' WHERE uid='.$uid.' AND abi_id='.$abi_id.';';
			}
			$ret = $DB->exeSql($sql);
		}
		
		
		$result['val'] = (boolean)$ret;
		if ($ret) {
			$DB->commit();
		} else {
			$DB->rollBack();
			$result['msg'] = $L['Failure'];
			$str = var_export($DB->errors, true);
			// 记录错误日志
			Log::save('equip_take_off', $str);
		}
		return $result;
	}
	
	/**
	 * 装备强化
	 * @param int $uid
	 * @param int $equip_id 背包物品ID
	 * @param int $luck_times 使用幸运石提升成功率的次数
	 */
	public static function strengthen($uid, $equip_id, $luck_times=0)
	{
		global $DB, $L, $G;
		
		$equip_id = (int)$equip_id;
		$luck_times = (int)$luck_times;
		if ($luck_times > 10) $luck_times = 10;
		
		$info = self::getInfo($uid, $equip_id);
		if (!$info || ($info['uid'] != $uid)) {
			return array(
				'val' => false,
				'msg' => $L['invalid_request']
			);
		}
		// 非装备不能强化
		if ($info['item_type'] != 53) {
			return array(
				'val' => false,
				'msg' => $L['invalid_request']
			);
		}
		// 要强化的装备必须在背包中
//		if ($info['status'] != 1) {
//			return array(
//				'val' => false,
//				'msg' => $L['equip_cannot_strengthen_off_bag']
//			);
//		}
		// 已经达到最高强化等级
		if ($info['strengthen_lvl'] == $G['max_strengthen_lvl']) {
			return array(
				'val' => false,
				'msg' => $L['equip_is_max_strengthen_lvl']
			);
		}
		// 金钱不足
		$sql = 'SELECT coin FROM user WHERE uid='.$uid;
		$coin = $DB->getValue($sql);
		$needCoin = $G['equip_strengthen_need_coin'][$info['strengthen_lvl']];
		if ($coin < $needCoin) {
			return array(
				'val' => false,
				'msg' => $L['coin_not_enough']
			);
		}
		
		$strengthen_lvl = $info['strengthen_lvl']+1;
		
		$successRate = $G['equip_strengthen_success_rate'][$strengthen_lvl];
	
		// 使用幸运石
		if ($luck_times) {
			$sql = 'SELECT bag_id, num FROM bag WHERE uid='.$uid.' AND item_id=10001';
			$luckStone = $DB->getValue($sql);
			// 幸运石是否足够
			if ($luckStone['num'] < $G['luck_stone_require_cfg'][$luck_times]) {
				return array(
					'val' => false,
					'msg' => $L['luck_stone_not_enough']
				);
			}
			$addSuccessRate = $G['add_success_rate_val_per_times'] * $luck_times;
			$successRate += $addSuccessRate;
			$successRate = (int)$successRate;
		}
		
		$success = getRandResult($successRate);
		$cfg = $G['equip_strengthen_add_abi'];
		$ret = true;
		
		
		$DB->beginTransaction();
		
		// 穿在身上装备先脱下
		if ($info['status'] == 2) {
			$DB->setTransactionState(false);
			self::takeOff($uid, $info['staff_id'], $equip_id);
		}
		
		// 强化成功更新强化等级和能力值
		if ($success) {
			foreach ($info['attr']['base'] as $k=>$v) {
				$info['attr']['result'][$k] = round($info['attr']['base'][$k] * (1 + $cfg[$strengthen_lvl]));
			}
			$attr = json_encode($info['attr']);
			$sql = 'UPDATE bag SET strengthen_lvl=strengthen_lvl+1,attr=\''.$attr.'\''
				  .' WHERE bag_id='.$equip_id;
			$ret = $DB->exeSql($sql);
			$info['strengthen_lvl']++;
		}
		
		// 穿在身上装备脱下后再穿上
		if ($info['status'] == 2) {
			self::putOn($uid, $info['staff_id'], $equip_id);
			$DB->setTransactionState(true);
		}
		
		// 消耗金钱
		if ($ret) {
			$sql = 'UPDATE user SET coin=coin-'.$needCoin.' WHERE uid='.$uid;
			$ret = $DB->exeSql($sql);
		}
		
		// 消耗幸运石
		if ($ret && $luck_times) {
			if ($luckStone['num'] == $G['luck_stone_require_cfg'][$luck_times]) {
				$ret = $DB->delete('bag', 'bag_id='.$luckStone['bag_id']);
			} else {
				$sql = 'UPDATE bag SET num=num-'.$G['luck_stone_require_cfg'][$luck_times]
						.' WHERE bag_id='.$luckStone['bag_id'];
				$ret = $DB->exeSql($sql);
			}
		}
		
		
		$result['val'] = (boolean)$ret;
		if ($ret) {
			$DB->commit();
			$info['strengthen_lvl'] = $strengthen_lvl;
			$result['data'] = array(
				'success' => $success,
				'equip' => $info
			);
		} else {
			$DB->rollBack();
			$result['msg'] = $L['Failure'];
			$str = var_export($DB->errors, true);
			// 记录错误日志
			Log::save('equip_strengthen', $str);
		}
		return $result;
	}
	
	/**
	 * 宝石合成
	 * @param int $uid
	 * @param array $gemList 要合成的宝石列表(背包中的ID)
	 * @param int $luck_times 使用幸运石提升成功率的次数
	 */
	public static function gemMix($uid, $gemList, $luck_times=0)
	{
		global $DB, $L, $G;
		
		$gemCfgList = C('item/gem');
		
		$luck_times = (int)$luck_times;
		if ($luck_times > 10) $luck_times = 10;
		
		$num = count($gemList);
		if ($num != 2) {
			return array(
				'val' => false,
				'msg' => $L['gem_mix_num_error']
			);
		}
		$idStr = '';
		foreach ($gemList as $id) {
			if (!is_int_string($id)) {
				return array(
					'val' => false,
					'msg' => $L['invalid_request']
				);
			}
			if ($idStr=='') {
				$idStr = $id;
			} else {
				$idStr .= ','.$id;
			}
		}
		
		$sql = 'SELECT * FROM bag WHERE bag_id in ('.$idStr.')';
		$list = $DB->getList($sql);
		// 提供的背包ID不存在
		if (!$list || $num > count($list)) {
			return array(
				'val' => false,
				'msg' => $L['invalid_request']
			);
		}
		$lvl = 0;
		$color = 0;
		$getGemId = 0;
		$attrList = array();
		foreach ($list as $gemInfo) {
			$attr = json_decode($gemInfo['attr'], true);
			$attrList = $attr + $attrList;
			$getGemId = $gemInfo['item_id'];
			// 提供的宝石不是玩家自己的
			if ($gemInfo['uid']!=$uid) {
				return array(
					'val' => false,
					'msg' => $L['invalid_request']
				);
			}
			// 提供的背包物品不是宝石
			if ($gemInfo['item_type']!=52) {
				return array(
					'val' => false,
					'msg' => $L['invalid_request']
				);
			}
			if ($lvl==0) {
				$lvl = $gemInfo['lvl'];
			} elseif ($lvl != $gemInfo['lvl']) {
				return array(
					'val' => false,
					'msg' => $L['all_gem_for_mix_must_be_same_lvl']
				);
			} elseif ($gemInfo['lvl'] >= $G['gem_max_lvl']) {
				return array(
					'val' => false,
					'msg' => $L['gem_is_max_lvl']
				);
			}
			if ($color==0) {
				$color = $gemInfo['color'];
			}
			// 颜色必须一致
			elseif ($color != $gemInfo['color']) {
				return array(
					'val' => false,
					'msg' => $L['all_gem_for_mix_must_be_same_color']
				);
			}
		}
	
		// 金钱不足
		$sql = 'SELECT coin FROM user WHERE uid='.$uid;
		$coin = $DB->getValue($sql);
		$needCoin = $G['gem_mix_need_coin'][$lvl];
		if ($coin < $needCoin) {
			return array(
				'val' => false,
				'msg' => $L['coin_not_enough']
			);
		}
		
		$successRate = $G['gem_mix_success_rate'][$lvl];
	
		// 使用幸运石
		if ($luck_times) {
			$sql = 'SELECT bag_id, num FROM bag WHERE uid='.$uid.' AND item_id=10001';
			$luckStone = $DB->getValue($sql);
			if ($luckStone['num'] < $G['luck_stone_require_cfg'][$luck_times]) {
				return array(
					'val' => false,
					'msg' => $L['luck_stone_not_enough']
				);
			}
			$addSuccessRate = $G['add_success_rate_val_per_times'] * $luck_times;
			$successRate = $successRate * (1 + $addSuccessRate);
			$successRate = (int)$successRate;
		}
		
		$success = getRandResult($successRate);
		if ($success) {
			$gemCfg = $gemCfgList[$getGemId];
			$abi_id = array_rand($attrList);
			$abi_val = $G['gem_lvl_val'][$lvl+1];
			$attr = array($abi_id => $abi_val);
			$getGem = array(
				'item_id'	=> $getGemId,
				'item_type'	=> $gemCfg['item_type'],//宝石类型
				'title'		=> $gemCfg['title'],
				'num'		=> 1,
				'lvl'		=> $lvl+1,
				'stack'		=> 0,
				'status'	=> 1,
				'color'		=> $gemCfg['color'],
				'attr'		=> json_encode($attr),
			);
		}
		
		$DB->beginTransaction();
		$ret = true;
		
		if ($success) {
			$ret = $DB->delete('bag', 'bag_id in ('.$idStr.')');
			if ($ret) {
				$ret = Bag::in($uid, $getGem);
			}
		} else {// 失败则随机消失任意一颗宝石
			$awayGemKey = array_rand($gemList);
			$awayGem = $gemList[$awayGemKey];
			$ret = $DB->delete('bag', 'bag_id='.$awayGem);
		}
		
		// 消耗金钱
		if ($ret) {
			$sql = 'UPDATE user SET coin=coin-'.$needCoin.' WHERE uid='.$uid;
			$ret = $DB->exeSql($sql);
		}
		
		// 消耗幸运石
		if ($ret && $luck_times) {
			if ($luckStone['num'] == $G['luck_stone_require_cfg'][$luck_times]) {
				$ret = $DB->delete('bag', 'bag_id='.$luckStone['bag_id']);
			} else {
				$sql = 'UPDATE bag SET num=num-'.$G['luck_stone_require_cfg'][$luck_times]
						.' WHERE bag_id='.$luckStone['bag_id'];
				$ret = $DB->exeSql($sql);
			}
		}
		
		$result['val'] = (boolean)$ret;
		if ($ret) {
			$DB->commit();
			$getGem['bag_id'] = Bag::$_bag_id;
			$getGem['attr'] = $attr;
			$result['data'] = array(
				'success' => $success
			);
			if ($success) {
				$result['data']['get_gem'] = $getGem;
			}
		} else {
			$DB->rollBack();
			$result['msg'] = $L['Failure'];
			$str = var_export($DB->errors, true);
			// 记录错误日志
			Log::save('gem_mix', $str);
		}
		return $result;
	}
	
	/**
	 * 宝石镶嵌
	 * @param int $uid
	 * @param int $equip_id 装备在背包中的ID
	 * @param int|array $gem_id 宝石在背包中的ID
	 * array(
	 * 	pos => $gem_id,
	 * 	pos => $gem_id,
	 * .....
	 * )
	 * @param int $pos 镶嵌装备的槽位顺序:1,2,3...
	 */
	public static function inlay($uid, $equip_id, $gem_id, $pos=null)
	{
		global $DB, $L, $G;
				
		$gemCfgList = C('item/gem');
		$equipCfgList = C('item/equip');
		
		$equip_id = (int)$equip_id;
		$onlyOne = true;
		
		if (is_int_string($gem_id)) {
			$sql = 'SELECT * FROM bag WHERE bag_id='.$gem_id;
			$gem = $DB->getValue($sql);
			$gemArr = array($gem);
		} else if (is_array($gem_id)) {
			$onlyOne = false;
			$ids = '';
			foreach ($gem_id as $pos => $id) {
				if (!is_int_string($id)) {
					return array(
						'val' => false,
						'msg' => $L['invalid_request']
					);
				}
				$ids .= $id.',';
			}
			$ids = substr($ids, 0, -1);
			$sql = 'SELECT * FROM bag WHERE bag_id in ('.$ids.')';
			$gemArr = $DB->getList($sql);
		} else {
			return array(
				'val' => false,
				'msg' => $L['invalid_request']
			);
		}
		
		// 获取装备信息
		$equip = self::getInfo($uid, $equip_id);
		
		// 镶嵌需要的金钱
		$needCoin = $G['gem_inlay_need_coin'][$equip['lvl']];
		$sql = 'SELECT coin FROM user WHERE uid='.$uid;
		$coin = $DB->getValue($sql);
		if ($coin < $needCoin) {
			return array(
				'val' => false,
				'msg' => $L['coin_not_enough']
			);
		}
		
		
		$colorSign = $G['id_color'][$equip['color']];
		if (count($equip['inlay']) > $G['equip_color_slot_num'][$colorSign]) {
			return array(
				'val' => false,
				'msg' => $L['equip_stone_slot_not_enough']
			);
		}
		// 装备不存在/非玩家装备/非装备
		if (!$equip || $equip['uid'] != $uid || $equip['item_type']!=53) {
			return array(
				'val' => false,
				'msg' => $L['invalid_request']
			);
		}
		
		foreach ($gemArr as $gem) {
			// 宝石属性
			$gem['attr'] = json_decode($gem['attr'], true);
			// 宝石不存在/非玩家宝石/非宝石
			if (!$gem || $gem['uid'] != $uid || $gem['item_type']!=52) {
				return array(
					'val' => false,
					'msg' => $L['invalid_request']
				);
			}
			// 装备和宝石的能力类型不一致
			$gemClass = $gemCfgList[$gem['item_id']]['class'];
			$equipClass = $equipCfgList[$equip['item_id']]['class'];
			if ($gemClass != $equipClass) {
				return array(
					'val' => false,
					'msg' => $L['gem_class_must_be_equal_to_equip']
				);
			}
			// 宝石等级不能大于装备等级
			if ($gem['lvl'] > $equip['lvl']) {
				return array(
					'val' => false,
					'msg' => $L['gem_lvl_must_equal_or_less_than_equip']
				);
			}
			
			if (!$onlyOne) {
				$pos = $gem_id[$gem['bag_id']];
			}
			
			if (!isset($equip['inlay'][$pos])) {
				return array(
					'val' => false,
					'msg' => $L['inlay_slot_not_exist']
				);
			}
			
			$equip['inlay'][$pos] = $gem['attr'];
		
		}
		$inlay = json_encode($equip['inlay']);
		
		
		$DB->beginTransaction();
		
		// 消耗金钱
		$sql = 'UPDATE user SET coin=coin-'.$needCoin.' WHERE uid='.$uid;
		$ret = $DB->exeSql($sql);
		
		// 宝石删除
		if ($ret) {
			if ($onlyOne) {
				$ret = $DB->delete('bag', 'bag_id='.$gem_id);
			} else {
				$sql = 'DELETE FROM bag WHERE bag_id in ('.$ids.')';
				$ret = $DB->exeSql($sql);
			}
			if (false !== $ret) {
				$ret = true;
			}
		}
		
		
		// 装备得到宝石的属性,并减少可镶嵌槽数
		if ($ret) {
			// 镶嵌属性不再加入最终效果保存
			if (isset($equip['attr']['result'][$gem['item_id']])) {
//				$equip['attr']['result'][$gem_item_id] += $gem['attr'][$gem_item_id];
				$data = array(
					'inlay' => $inlay,
//					'attr'	=> json_encode($equip['attr'])
				);
			} else {
//				$equip['attr']['result'][$gem_item_id] = $gem['attr'][$gem_item_id];
				$data = array(
					'inlay' => $inlay,
//					'attr'	=> json_encode($equip['attr'])
				);
			}
			$ret = $DB->update('bag', $data, 'bag_id='.$equip_id);
			if (false !== $ret) {
				$ret = true;
			}
		}
		
		
		$result['val'] = (boolean)$ret;
		if ($ret) {
			$DB->commit();
			$result['data'] = $equip;
		} else {
			$DB->rollBack();
			$result['msg'] = $L['Failure'];
			$str = var_export($DB->errors, true);
			// 记录错误日志
			Log::save('equip_strengthen', $str);
		}
		return $result;
	}
}




